package com.ruoyi.project.ins.service.impl;

import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.ins.domain.InsAccount;
import com.ruoyi.project.ins.domain.InsBatchMessage;
import com.ruoyi.project.ins.domain.InsMessage;
import com.ruoyi.project.ins.domain.InsWebProxy;
import com.ruoyi.project.ins.service.IInsBatchMessageService;
import com.ruoyi.project.mapper.InsAccountMapper;
import com.ruoyi.project.mapper.InsBatchMessageMapper;
import com.ruoyi.project.mapper.InsMessageMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.Validator;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class InsBatchMessageServiceImpl implements IInsBatchMessageService {

    @Autowired
    private InsBatchMessageMapper insBatchMessageMapper;

    @Autowired
    private InsMessageMapper insMessageMapper;

    @Autowired
    private InsAccountMapper insAccountMapper;

    @Autowired
    protected Validator validator;

    @Override
    public List<InsBatchMessage> selectBatchMessageList(InsBatchMessage batchMessage) {
        return insBatchMessageMapper.selectBatchMessageList(batchMessage);
    }

    @Override
    public int insertBatchMessage(InsBatchMessage batchMessage) {
        return insBatchMessageMapper.insertBatchMessage(batchMessage);
    }

    @Override
    public int updateBatchMessage(InsBatchMessage batchMessage) {
        return insBatchMessageMapper.updateBatchMessage(batchMessage);
    }

    @Override
    public InsBatchMessage selectBatchMessageById(Long id) {
        return insBatchMessageMapper.selectBatchMessageById(id);
    }

    @Override
    public AjaxResult sendBatchMessage(InsBatchMessage batchMessage, List<InsAccount> accountList, InsWebProxy insWebProxy) {
        int sendTimes = batchMessage.getSendTimes();
        // 插入批量发送消息记录失败
        batchMessage.setUserNums(accountList.size());
        batchMessage.setTotalNums(accountList.size() * sendTimes);
        batchMessage.setFailedNums(0);
        batchMessage.setSuccessNums(0);
        int rows = insBatchMessageMapper.insertBatchMessage(batchMessage);
        if (rows <= 0 || batchMessage.getId() == null) {
            return AjaxResult.error("插入批量发送消息记录失败");
        }
        // 生成所有待发送消息
        List<InsMessage> batchInsertList = new ArrayList<>(accountList.size() * sendTimes);
        for (InsAccount account : accountList) {
            account.setSendTimes(account.getSendTimesOrDefault() + sendTimes);
            InsMessage message = new InsMessage();
            message.setSendUserid(account.getUserId());
            message.setSendUsername(account.getUsername());
            message.setMessageType(batchMessage.getMessageType());
            message.setMessageContent(batchMessage.getMessageContent());
            message.setShareUserid(batchMessage.getShareUserid());
            message.setStatus("0");
            message.setFailTimes(0);
            message.setSendType("1");
            // 必须等batch插入后才有值
            message.setBatchId(batchMessage.getId());
            message.setCreateBy(batchMessage.getCreateBy());
            message.setProxyServer(insWebProxy.getProxyServer());
            message.setProxyPort(insWebProxy.getProxyPort());
            message.setProxyCustomer(insWebProxy.getProxyCustomer());
            message.setProxyPassword(insWebProxy.getProxyPassword());
            for (int i = 0; i < sendTimes; i++) {
                batchInsertList.add(message);
            }
        }
        // 插入消息任务
        rows = insMessageMapper.batchInsertInsMessage(batchInsertList);
        if (rows <= 0) {
            return AjaxResult.error("插入消息详细信息失败");
        }
        // 批量更新档案用户发消息次数
        insAccountMapper.batchUpdateSendTimes(accountList);
        return AjaxResult.success("批量消息发送任务创建成功");
    }
}
